package com.cbts.service.info.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;



import com.cbts.dao.DepartmentDao;
import com.cbts.entity.Department;
import com.cbts.service.info.DepartmentService;
import com.cbts.util.Page;
/**
 * @author 邱鸿辉
 *
 */
public class DepartmentServiceImpl implements DepartmentService{
	private DepartmentDao departmentDao;

	
	public DepartmentDao getDepartmentDao() {
		return departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public boolean addDepartment(Department department) {
		
		if(departmentDao.save(department))
			return true;
		return false;
	}

	public boolean deleteDepartment(Department department) {
		
		if(departmentDao.delete(department))
			return true;
		return false;
	}

	public Department getDepartment(String id) {
		
		
		return (Department)departmentDao.get(id);
	}

	public boolean updateDepartment(Department department) {
		
		if(departmentDao.update(department))
			return true;
		return false;
	}
	public List<Department> getAllDepartment() {
		
		return departmentDao.findAll().getPageItems();
	}
	public Page<Department> getAllDepartmentByPage(){
		return departmentDao.findAll();
	}
	

	public void updateDepartmentList(List<Department> departmentList) {
		 departmentDao.updateDepartmentList(departmentList);
	}

	public boolean deleteDepartmentList(List<Department> departmentList) {
		
		if(departmentDao.deleteList(departmentList))
			return true;
		return false;
	}

	public List<Department> getSelectedDepartmentList(List<Department> departmentList,String check,String operate) {
		
		try {
				String[] arraycheckbox = check.split(", ");//因为被用户勾选的部门的departmentId被封装成字符串check,
															//这里对check进行分割，得到被勾选的部门的departmentId
				ArrayList<Department> selectedList = new ArrayList<Department>();//用于存储用户勾选的部门
	
				for (int i = 0; i < arraycheckbox.length; i++)
				{
					Department department = departmentDao.get(arraycheckbox[i]);//根据arraycheckbox[i]，即departmentId得到部门
					if ("修改".equals(operate)&&!departmentList.isEmpty())
					{
						for (int j = 0; j < departmentList.size(); j++)
						{
							if (departmentList.get(j).getDepartmentId().equals(arraycheckbox[i]))
							{
								department.setDepartmentName(departmentList.get(j).getDepartmentName());//修改部门名称
								break;
							}
						}
					}
					selectedList.add(department);		
				}
				return  selectedList;
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
	}
	public Department getDepartmentByDepartmentName(String departmentName){
		
		return departmentDao.getDepartmentByDepartmentName(departmentName);
	}
	public boolean sameDepartmentNameExist(String departmentId,String departmentName){
		String hql = "and o.departmentId != ? and o.departmentName = ?";
		return departmentDao.queryUnique(hql, new Object[]{departmentId,departmentName}) != null;
	}
}
